UICommonPop = {}
HelperFunc.SetUIEnv(UICommonPop)
local this = UICommonPop
this.msgTable = {};
this.hasInit =false;
this.msgid = 0;

this.cancelmat = nil;
this.okmat = nil;
this.curTime = 0
this.timer = nil
this.clickTime = 0;
--初始化,id：显示文字配置id，callback:确认回调函数，参数(true or false ),...显示文字需要的参数
function UICommonPop.Init(id, callBack,...)
    msgid = id
	this.RecordMsg(id, callBack, ...)
    if #msgTable <= 0 then
        return ;
    end
	local name, packageName = getUIName(PanelResNames.UICommonPop);
	panelMgr:ShowUI(name, packageName, this.OnCreate,Config.useLocalUI);
end

--组件赋值
function UICommonPop.OnCreate(obj)
	if not hasInit then
		hasInit = true;
		this.lab_content = obj:GetChild("Pop")-- :GetComponent("TextMeshProUGUI");
		this.lab_title = obj:GetChild("TitleLb")-- :GetComponent("TextMeshProUGUI");
		this.btn_cancel = obj:GetChild("CancelBtn");
        this.btn_bgClose = obj:GetChild("bgClose");

        cancelmat = this.btn_cancel.title -- :Find("CancelBtnTips")-- :GetComponent("TextMeshProUGUI").fontMaterial;

		this.btn_confirm = obj:GetChild("ConfirmBtn");
		this.showtipsController = obj:GetController("showtips");
        this.showtipsController.selectedIndex = 0;
		this.today = obj:GetChild("today"):GetController("button");

        okmat = this.btn_confirm.title -- :Find("ConfirmBtnTips")-- :GetComponent("TextMeshProUGUI").fontMaterial;

        this.btn_close = obj:GetChild("CloseBtn");
		this.btn_close.visible = (false);
        this.timeText = obj:GetChild("Time")-- :GetComponent("TextMeshProUGUI");
        this.timeText.visible = (false)
        -- timer = Timer.New(this.UpdataTime, 1, -1, false)
        -- timer:Stop()
		this.ShowConfirm();
	end
end

function UICommonPop.getKey(id, key)
    key = key or "Time"
    return "UICommonPop_"..key.."_"..id;
end
function UICommonPop.RecordMsg(id, callBack, ...)
	local msg = {}
	msg["id"] = tostring(id)
	msg["callback"] = callBack
	
    local msgdata = G.dataTable["t_commonpop"][tostring(id)];
    local flag = false;
    if msgdata.Show == 1 then
        if GetPlayerPrefsTodayFlag(this.getKey(id)) then
            flag = true;
        end
    elseif msgdata.Show == 2 then
        if GetPlayerPrefs(this.getKey(id)) == "true" then
            flag = true;
        end
    else
        SetPlayerPrefs(this.getKey(id), "false");
    end
    if flag then
        if callBack then
            callBack(GetPlayerPrefs(this.getKey(id,"Key")) == "true");
        end
        return ;
    end
	local title = getLanguage(msgdata.Title);
    local content= getLanguage(msgdata.Tips);
    content = string.gsub(content,"\\n","\n",9);
    msg["title"] = title;
    if ... ~= nil then
        msg["content"] = string.format(content,...);
    else
        msg["content"] = content;
    end

	table.insert(msgTable, msg)
end

function UICommonPop.ShowConfirm()
	if #msgTable > 0 then
		this.SetInfo(msgTable[1]);
    else
        hasInit = false;
        this.OnClose();
	end
end

function UICommonPop.setKey(id, tab, flag)
    if this.today.selectedIndex == 0 then
        return ;
    end
    if tab == 1 then
        SetPlayerPrefsTodayFlag(this.getKey(id), true)
    elseif tab == 2 then
        SetPlayerPrefs(this.getKey(id), "true");
    end
    SetPlayerPrefs(this.getKey(id,"Key"), "true")
end

--设置数据
function UICommonPop.SetInfo(tab)
	local id = tab["id"];
	local callBack = tab["callback"];
	this.lab_title.text = tab["title"];
	this.lab_content.text = tab["content"];
    local msgdata = G.dataTable["t_commonpop"][tostring(id)];
    this.btn_cancel.visible = (false);
    this.btn_confirm.visible = (false);
    if(msgdata ~= nil) then

        if(msgdata.Type == 1 or msgdata.Type == 4) then
            this.btn_close.visible = (true);
            this.btn_close.title = getLanguage(msgdata.MBtn);
        end
        if msgdata.Type == 2 or msgdata.Type == 3 or msgdata.Type == 4 then
            this.btn_confirm.visible = (true);
            this.btn_cancel.visible = (true);
            if(msgdata.Type == 2) then
                this.btn_cancel.title = getLanguage(msgdata.RBtn, nil, okmat);
                this.btn_confirm.title = getLanguage(msgdata.LBtn, nil, cancelmat);
            else
                this.btn_cancel.title =getLanguage(msgdata.LBtn, nil, cancelmat);
                this.btn_confirm.title = getLanguage(msgdata.RBtn, nil, okmat);
            end
        end
        this.showtipsController.selectedIndex = msgdata.Show or 0;
        this.today.selectedIndex = 0;
    end
    this.btn_bgClose.onClick:Clear();
    this.btn_confirm.onClick:Clear();
    this.btn_close.onClick:Clear();
    this.btn_cancel.onClick:Clear();

    this.btn_close.onClick:Add(function()
        this.setKey(id, msgdata.Show, msgdata.Type == 1 or msgdata.Type == 4);
        this.OnClose()
        if callBack ~= nil then
            callBack(msgdata.Type == 1 or msgdata.Type == 4);
        end
    end);

    this.btn_confirm.onClick:Add(function()
        this.setKey(id, msgdata.Show, msgdata.Type == 3);
        this.OnClose()
        if callBack ~= nil then
            callBack(msgdata.Type == 3);
		end
    end);

    this.btn_cancel.onClick:Add(function()
        this.setKey(id, msgdata.Show, msgdata.Type == 2);
        this.OnClose()
        if callBack ~= nil then
            callBack(msgdata.Type == 2);
		end
	end);

    this.btn_bgClose.onClick:Add(function()
        this.setKey(id, msgdata.Show, msgdata.Type == 2);
        this.OnClose()
        if callBack ~= nil then
            callBack(msgdata.Type == 2,true);
        end
    end);
end

--取消
function UICommonPop.OnClose()
    if this.btn_confirm~=nil then
        this.btn_confirm.onClick:Clear();
    end
    this.btn_cancel.onClick:Clear();
    if this.timer ~= nil then
        this.timer:Stop()
        this.timer = nil
    end
	table.remove(msgTable,1);  
	if #msgTable > 0 then
        coroutine.start(this.InitInfo);
	else
        hasInit = false;
        closeUI(PanelResNames.UICommonPop);
	end

end

function UICommonPop.OnDisable()
    msgTable = {};
    if this.timer ~= nil then
        this.timer:Stop()
        this.timer = nil
    end
end
function UICommonPop.InitInfo()
    coroutine.wait(0.05)
    this.SetInfo(msgTable[1])
end

function UICommonPop.UpdataTime()
    if curTime > 0 then
        curTime = curTime -1
        if this.timeText then
            this.timeText.text = "剩余：" .. curTime .. "秒"
        end
        if curTime <= 0 then
            this.OnClose()
        end
    end
end

function UICommonPop.SetTime(time)
    if time == nil or time <= 0 then
        return
    end
    curTime = time
    if this.timeText ~= nil then
        this.timeText.visible = true
        this.timeText.text = "剩余：" .. curTime .. "秒"
    end
    if this.timer == nil then
        this.timer = Timer.New(this.UpdataTime, 1, -1, true)
    end
    this.timer:Start()
end
return UICommonPop